/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.dxo;

import java.util.HashMap;

import cc.aileron.accessor.PojoAccessorDxo;
import cc.aileron.commons.utils.CastUtils;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * 
 * 数値の型自動変換
 * 
 * @author Aileron
 * 
 */
@Singleton
public class NumberDxo implements
        PojoAccessorDxo.Setter<java.lang.Number, Object>
{
    private static interface C
    {
        Object convert(final java.lang.Number number);
    }

    /**
     * @param parameter
     * @param resultClass
     * @return number
     */
    public Object convert(final java.lang.Number parameter,
            final Class<?> resultClass)
    {
        return setter(parameter, CastUtils.<Class<Object>> cast(resultClass));
    }

    public Object setter(final java.lang.Number parameter,
            final Class<Object> resultClass)
    {
        final C c = map.get(resultClass.getName());
        if (c == null)
        {
            return parameter;
        }
        return c.convert(parameter);
    }

    /**
     * number
     */
    @Inject
    public NumberDxo()
    {
        map = new HashMap<String, C>();
        map.put("boolean", new C()
        {
            public Boolean convert(final java.lang.Number number)
            {
                return number.intValue() != 0;
            }
        });
        map.put("byte", new C()
        {

            public Byte convert(final java.lang.Number number)
            {
                return number.byteValue();
            }
        });
        map.put("short", new C()
        {

            public Short convert(final java.lang.Number number)
            {
                return number.shortValue();
            }
        });
        map.put("int", new C()
        {
            public Integer convert(final java.lang.Number number)
            {
                return number.intValue();
            }

        });
        map.put("long", new C()
        {

            public Long convert(final java.lang.Number number)
            {
                return number.longValue();
            }
        });
        map.put("float", new C()
        {

            public Float convert(final java.lang.Number number)
            {
                return number.floatValue();
            }
        });
        map.put("double", new C()
        {

            public Double convert(final java.lang.Number number)
            {
                return number.doubleValue();
            }
        });
    }

    private final HashMap<String, C> map;
}
